package com.visual.controller;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import java.io.*;

public class MainActivity extends AppCompatActivity {
    final static String TAG = "Screencap";
    Thread screencapThread;
    boolean threadRunning = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    protected void onResume() {
        super.onResume();
        startThread();
    }

    @Override
    protected void onStop() {
        super.onStop();
        threadRunning = false;
        screencapThread.interrupt();
        screencapThread = null;
    }

    private void startThread() {
        screencapThread = new Thread(new Runnable() {
            @Override
            public void run() {
                while (threadRunning) {
                    try {
                        long time1, time2;
                        time1 = System.currentTimeMillis();
                        String commandStr = "screencap -p";
//                        Process p = Runtime.getRuntime().exec(new String[]{"/system/bin/su","-c", commandStr});
                        Process process = Runtime.getRuntime().exec("su\n");
                        DataOutputStream os = new DataOutputStream(process.getOutputStream());
                        os.writeBytes(commandStr + "\n");
                        os.flush();
                        process.waitFor();
                        /*"标准输出流"就在当前方法中读取*/
                        BufferedInputStream bis = new BufferedInputStream(process.getInputStream());
                        ByteArrayOutputStream bout = new ByteArrayOutputStream();
                        FileOutputStream fout = new FileOutputStream("/sdcard/sc00.png");
                        time2 = System.currentTimeMillis();
                        System.out.println("\n\n1 cmd time:" + (time2 - time1));
                        byte[] buffer = new byte[1];
                        while (bis.read(buffer) > 0) {
                            fout.write(buffer);
                            bout.write(buffer);
                        }
                        System.out.println(new String(bout.toByteArray(),"utf-8"));
                        fout.flush();
                        fout.close();
                        time1 = System.currentTimeMillis();
                        System.out.println("2 read buffer time:" + (time1 - time2));
                        ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray());
                        DataInputStream dis = new DataInputStream(bin);
                        Bitmap bitmap = BitmapFactory.decodeStream(dis);
                        time2 = System.currentTimeMillis();
                        System.out.println("3 buffer2img time:" + (time2 - time1));
                        saveBitmap("sc01.png", bitmap);
                    } catch (IOException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
        screencapThread.setName("screencap");
        screencapThread.start();
    }

    public void saveBitmap(String fileName, Bitmap bm) {
        Log.d(TAG, "保存图片");
        if(bm == null){
            Log.d(TAG, "Bitmap is null");
            return;
        }
        File f = new File("/sdcard/", fileName);
        if (f.exists()) {
            f.delete();
        }
        try {
            FileOutputStream out = new FileOutputStream(f);
            bm.compress(Bitmap.CompressFormat.PNG, 90, out);
            out.flush();
            out.close();
            Log.i(TAG, "已经保存");
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
